
let states = null
let timer = null; // 记录上一次的延时器  
let tim = 30*60*1000   // 延迟的时间


// 文件上传
const uploadFile = opt => {
	return new Promise((resolve, reject) => {
		opt = opt || {};
		opt.url = opt.url || '';
		opt.filePath = opt.filePath || null; //要上传文件资源的路径。   
		opt.name = opt.name || null; //文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容 
		//opt.filePath = opt.filePath || null;
		opt.data = opt.data || null;
		opt.header = opt.header || {
			"Content-Type": "multipart/form-data",
			"Content-Type": "application/json"
		};

		opt.success = opt.success || function() {};

		uni.uploadFile({
			url: opt.url,
			filePath: opt.filePath,
			name: opt.name,
			formData: opt.data,
			header: opt.header,
			success: function(res) {
				resolve(res);
			},
			fail: function(err) {
				reject(err)
				uni.showToast({
					title: '请稍后重试'
				});
			}
		})
	})
}

// 请求
const http = (opt) => {
	return new Promise((resolve, reject) => {
		opt = opt || {};
		opt.url = opt.url || '';
		opt.timeout = opt.timeout || 30000;
		opt.data = opt.data || null;
		opt.method = opt.method || 'GET';
		opt.header = opt.header || {
			"Content-Type": "application/x-www-form-urlencoded",
			"Content-Type": "application/json"
		};
		// 对统一的请求进行处理
		let obj = {}
		let reqid = uni.request({
			url: opt.url,
			data: opt.data,
			method: opt.method,
			timeout: opt.timeout,
			header: opt.header,
			dataType: 'json',
			success: function(res) {
				// console.log('响应数据', res)
				/*if(res.data.errors){
					return uni.showToast({
						title:res.data.errors[0].message,
						icon:'none'
					})
				}*/
				resolve(res)
			},
			fail: function(err) {
				reject(err)
				console.log('错误请求',opt.url)
				uni.showToast({
					title: '请稍后重试!',
					icon: 'none',
					duration: 2500
				});
			}
		})
		states = reqid
	})
}

export default {
	http,
	uploadFile
}
